<template>
    <view class="pages">
        <view class="list-item" v-for="item in UserList" :key="item.id" @click="addAitUser(item)">
            <view class="list-item-left">
                <img :src="item.basicUserInfo.photo_url"/>
            </view>
            <view class="list-item-center">
                <text>{{item.basicUserInfo.nickname}}</text>
            </view>
            <view class="list-item-right">
                <img src="/static/img/right.png" />
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        name: "selectUser",
        data(){
            return{
                UserList:[],
                postContent:'',
                altUserList:[] //alt总的数据
            }
        },
        onLoad(query){
            console.log(query)
            this.postContent = query.post_content;
            this.altUserList = JSON.parse(query.alt_user_list)
        },
        onShow(){
          this.getFollowUserList();
        },
        methods:{
            //获取好友列表
            async getFollowUserList(){
                let params = {
                    page: 1,
                    limit: 10,
                    user_token: uni.getStorageSync('userToken')
                }
                const res = await this.$api.getFollowUserList(params);
                if (res.data.level == 'success'){
                    this.UserList = res.data.data;
                    if (res.data.data.length==0){
                        uni.showToast({
                            icon:'none',
                            title:'您还没有关注其他哔友哦~'
                        })
                    }
                } else {
                    uni.showToast({
                        icon:'none',
                        title:res.data.message
                    })
                }
            },
            addAitUser(user){
                //显示的文本添加@
                let pages = getCurrentPages();
                let prevPage = pages[pages.length - 2];
                if(this.altUserList.length == 0){
                    this.altUserList.push(user);
                    prevPage.setData({
                        altUserLists: this.altUserList,
                    })
                    uni.navigateBack({
                        delta: 1
                    })
                }else {
                    let hastIn=this.altUserList.find(function (item){
                        return item.basicUserInfo.nickname == user.basicUserInfo.nickname;
                    });
                    if(hastIn == undefined){
                        this.altUserList.push(user);
                        prevPage.setData({
                            altUserList: this.altUserList
                        })
                        uni.navigateBack({
                            delta: 1
                        })
                    }else {
                        uni.showToast({
                            icon:'none',
                            title:"请勿重复@该用户"
                        })
                    }
                }
            },
        }
    }
</script>

<style scoped lang="scss">
    .pages{
        padding: 3vw;
        display: flex;
        flex-direction: column;
        .list-item{
            display: flex;
            flex-direction: row;
            justify-content: left;
            padding: 10rpx 0;
            .list-item-left{
                width: 15vw;
                height: 15vw;
                border-radius: 360rpx;
            }
            .list-item-center{
                width: 60vw;
                display: flex;
                align-items: center;
                padding: 0 2vw;
            }
            .list-item-right{
                width: 7vw;
                height: 7vw;
                padding: 4vw;
            }
        }
    }
</style>